package com.sc.nft.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.entity.MallDrawRecord;
import com.sc.nft.enums.draw.MallDrawStatus;
import com.sc.nft.mapper.MallDrawRecordMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public class MallDrawRecordDao extends ServiceImpl<MallDrawRecordMapper, MallDrawRecord> {

    public List<MallDrawRecord> listByUserIdActIdAndStatus(Long userId, Long activityId, MallDrawStatus drawStatus) {
        LambdaQueryWrapper<MallDrawRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(MallDrawRecord::getUserId, userId).eq(MallDrawRecord::getActivityId, activityId)
                .eq(MallDrawRecord::getDrawStatus, drawStatus.getCode()).orderByAsc(MallDrawRecord::getPoolIndex);
        return list(queryWrapper);
    }

    public Page<MallDrawRecord> listUserDrawRecord(Long userId, Page page) {
        return this.listUserDrawRecord(null, null, null, null, userId, page);
    }

    public Page<MallDrawRecord> listUserDrawRecord(Long id, String prizeName, Date startTime, Date endTime, Long userId, Page page) {
        LambdaQueryWrapper<MallDrawRecord> queryWrapper = Wrappers.lambdaQuery();
        if (startTime != null || endTime != null) {
            queryWrapper.between(MallDrawRecord::getCreateTime, startTime, endTime);
        }
        if (StringUtils.isNotEmpty(prizeName)) {
            queryWrapper.ne(MallDrawRecord::getDrawStatus, MallDrawStatus.NOT_WIN.getCode());
            queryWrapper.like(MallDrawRecord::getPrizeInfo, prizeName);
        }
        queryWrapper.eq(userId != null && userId > 0, MallDrawRecord::getUserId, userId).eq(MallDrawRecord::getParentId, 0)
                .orderByDesc(MallDrawRecord::getId);
        if (id != null) {
            queryWrapper.eq(MallDrawRecord::getActivityId, id);
        }
        return page(page, queryWrapper);
    }

    public List<MallDrawRecord> listByUserIdAndParentId(Long userId, Long activityId, Long parentId) {
        LambdaQueryWrapper<MallDrawRecord> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(MallDrawRecord::getUserId, userId).eq(MallDrawRecord::getActivityId, activityId)
                .eq(MallDrawRecord::getParentId, parentId).orderByAsc(MallDrawRecord::getPoolIndex);
        return list(queryWrapper);
    }

}
